YUI.add('slide-control', function (Y) {
    /* Any frequently used shortcuts, strings and constants */
    var Lang = Y.Lang,
    Widget = Y.Widget,
    Node = Y.Node;

    var Lang = Y.Lang,
	WidgetStdMod = Y.WidgetStdMod;

    var StandardModule = Y.Base.create("standardModule", Y.Widget, [Y.WidgetPosition,Y.WidgetPositionAlign]);

    /* SlideControl class constructor */
    function SlideControl(config) {
        SlideControl.superclass.constructor.apply(this, arguments);
    }

    /* 
     * Required NAME static field, to identify the class and 
     * used as an event prefix, to generate class names etc. (set to the 
     * class name in camel case). 
     */
    SlideControl.NAME = "SlideControl";
	/*
	 * The attribute configuration for the SlideControl widget. Attributes can be
	 * defined with default values, get/set functions and validator functions
	 * as with any other class extending Base.
	 */
	SlideControl.ATTRS = {
	    // The minimum value for the SlideControl.
	    // override default ("null"), required for focus()
	    tabIndex: {
	        value: 0
	    },
	    
	    interval : {
	    	value : 3000
	    },
	    zIndex : {
	    	value : 0
	    },
	    autoSlide : {
	    	value : false
	    },
	
	    // The strings for the SlideControl UI. This attribute is 
	    // defined by the base Widget class but has an empty value. The
	    // SlideControl is simply providing a default value for the attribute.
	    strings: {
	        value: {
	            tooltip: "Press the arrow up/down keys for minor increments, page up/down for major increments.",
	            sure: "sure",
	            cancel: "cancel"
	        }
	    }
	};

	/* 
	 * The HTML_PARSER static constant is used by the Widget base class to populate 
	 * the configuration for the SlideControl instance from markup already on the page.
	 *
	 * The SlideControl class attempts to set the value of the SlideControl widget if it
	 * finds the appropriate input element on the page.
	 */
	SlideControl.HTML_PARSER = {
//	    file: function (srcNode) {
//	        return srcNode.one('.' + SlideControl.FILE_CLASS); 
//	    }
	};
	
	/* SlideControl extends the base Widget class */
	Y.extend(SlideControl, StandardModule, {
	
	    /*
	     * initializer is part of the lifecycle introduced by 
	     * the Widget class. It is invoked during construction,
	     * and can be used to setup instance specific state.
	     * 
	     * The SlideControl class does not need to perform anything
	     * specific in this method, but it is left in as an example.
	     */
	    initializer: function() {
	        // Not doing anything special during initialization
	    	
	    	this.timer = null;
	    	this.get('boundingBox').setStyle('zIndex',this.get('zIndex'));
            this.publish("slideMove", {
                defaultFn: this._defMyEventFn,
                bubbles:false
             });
	    },
	
	    /*
	     * destructor is part of the lifecycle introduced by 
	     * the Widget class. It is invoked during destruction,
	     * and can be used to cleanup instance specific state.
	     * 
	     * The SlideControl cleans up any node references it's holding
	     * onto. The Widget classes destructor will purge the 
	     * widget's bounding box of event listeners, so SlideControl 
	     * only needs to clean up listeners it attaches outside of 
	     * the bounding box.
	     */
	    destructor : function() {
	    	
	    },
	    
	    /*
	     * renderUI is part of the lifecycle introduced by the
	     * Widget class. Widget's renderer method invokes:
	     *
	     *     renderUI()
	     *     bindUI()
	     *     syncUI()
	     *
	     * renderUI is intended to be used by the Widget subclass
	     * to create or insert new elements into the DOM. 
	     *
	     * For SlideControl the method adds the input (if it's not already 
	     * present in the markup), and creates the inc/dec buttons
	     */
	    renderUI : function() {
	    	var contentBox = this.get("contentBox"),
	    		boundingBox = this.get('boundingBox');
	    	var prevBt = this.prevBt = Y.Node.create('<div style="float: left;"  title="" class="slidecontrol-prev"></div>'),
	    		nextBt = this.nextBt = Y.Node.create('<div style="float: right;"  class="slidecontrol-next disabled"></div>'),
	    		playPauseBt = this.playPauseBt = Y.Node.create('<div style="margin: auto; width:3px;"></div>');
	    	contentBox.appendChild(this.prevBt);
	    	contentBox.appendChild(this.nextBt);
	    	contentBox.appendChild(this.playPauseBt);
//	    	prevBt.setStyle('background','url("/m3958-module-css/slidecontrol.png") repeat scroll -59px -123px transparent');
//	    	nextBt.setStyle('background','url("/m3958-module-css/slidecontrol.png") repeat scroll -81px -123px transparent');
//	    	nextBt.setStyle('opacity',0.5);
	    	//background: url("/fd/s/a/h9.png") repeat scroll -81px -123px transparent;
	    	var vr = contentBox.get('viewportRegion');
	    	var r = boundingBox.get('region');
	    	this.set('xy',[vr.width - this.get('width') - 25,vr.height - r.height - 40]);
	    },
	
	    /*
	     * bindUI is intended to be used by the Widget subclass 
	     * to bind any event listeners which will drive the Widget UI.
	     * 
	     * It will generally bind event listeners for attribute change
	     * events, to update the state of the rendered UI in response 
	     * to attribute value changes, and also attach any DOM events,
	     * to activate the UI.
	     * 
	     * For SlideControl, the method:
	     *
	     * - Sets up the attribute change listener for the "value" attribute
	     *
	     * - Binds key listeners for the arrow/page keys
	     * - Binds mouseup/down listeners on the boundingBox, document respectively.
	     * - Binds a simple change listener on the input box.
	     */
	    bindUI : function() {
	    	this.prevBt.on('click',this.backToPrev,this);
	    	this.nextBt.on('click',this.gotoNext,this);
	    	this.after("autoSlideChange", this._afterAutoSlideChange);
//	    	this.fire('slideMove',{action:'next'});
	    },
	
	    /*
	     * syncUI is intended to be used by the Widget subclass to
	     * update the UI to reflect the current state of the widget.
	     * 
	     * For SlideControl, the method sets the value of the input field,
	     * to match the current state of the value attribute.
	     */
	    syncUI : function() {
	    	
	    },
	    
	    changeUI : function(e){
	    	var leftNum = e.leftNum,
	    		rightNum = e.rightNum;
	    	if(leftNum == 0){
	    		this.prevBt.addClass('disabled');
	    		this.prevBt.set('title','');
	    	}else{
	    		this.prevBt.removeClass('disabled');
	    		this.prevBt.set('title','前面还有' + leftNum + '张');
	    	}
	    	if(rightNum == 0){
	    		this.nextBt.addClass('disabled');
	    		this.nextBt.set('title','');
	    	}else{
	    		this.nextBt.removeClass('disabled');
	    		this.nextBt.set('title','后面还有' + rightNum + '张');
	    	}
	    },
	    backToPrev : function(e){
	    	if(e)e.preventDefault();
	    	this.fire('slideMove',{action:'prev'});
	    },
	    gotoNext : function(e){
	    	if(e)e.preventDefault();
	    	this.fire('slideMove',{action:'next'});
	    },
	    _defMyEventFn : function(){
	    	
	    }
	});
    Y.namespace("M3958").SlideControl = SlideControl;

},'0.0.1',{requires:['widget',"widget-position","widget-position-align"]});